﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreBDS
{
    public class ProjectBll
    {
        public static int Insert(Cvk_Project info, string strConnect)
        {
            int result = 0;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    db.Cvk_Projects.InsertOnSubmit(info);
                    db.SubmitChanges();
                    result = info.ProjectId;
                }
                catch { }
                finally { db.Dispose(); }
            }
            return result;
        }
        public static bool Delete(int id, string strConnect)
        {
            bool result = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    Cvk_Project info = db.Cvk_Projects.Single(p => p.ProjectId == id);
                    if (info != null)
                    {
                        db.Cvk_Projects.DeleteOnSubmit(info);
                        db.SubmitChanges();
                        result = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return result;
        }
        public static void Update(Cvk_Project info, string strConnect)
        {
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    Cvk_Project infoOld = db.Cvk_Projects.Single(p => p.ProjectId == info.ProjectId);
                    if (infoOld != null)
                    {
                        infoOld.Name = info.Name;
                        infoOld.Note = info.Note;
                        infoOld.Address = info.Address;
                        infoOld.Info = info.Info;
                        infoOld.Email = info.Email;
                        infoOld.Fax = info.Fax;
                        infoOld.Img = info.Img;
                        infoOld.IsActive = info.IsActive;
                        infoOld.Phone = info.Phone;
                        infoOld.Nick = info.Nick;
                        infoOld.Website = info.Website;
                        infoOld.HaTangQuyHoach = info.HaTangQuyHoach;
                        infoOld.ThietkeMaunha = info.ThietkeMaunha;
                        infoOld.ViTri = info.ViTri;
                        infoOld.BanHang = info.BanHang;
                        db.SubmitChanges();
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
        }
        public static bool UpdateDeleted(int id, bool isDeleted, string strConnect)
        {
            bool result = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    Cvk_Project infoOld = db.Cvk_Projects.Single(p => p.ProjectId == id);
                    if (infoOld != null)
                    {
                        infoOld.IsDeleted = isDeleted;
                        db.SubmitChanges();
                        result = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return result;
        }
        public static bool UpdateActived(int id, bool isActive, string strConnect)
        {
            bool result = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    Cvk_Project infoOld = db.Cvk_Projects.Single(p => p.ProjectId == id);
                    if (infoOld != null)
                    {
                        infoOld.IsActive = isActive;
                        db.SubmitChanges();
                        result = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return result;
        }

        public static Cvk_Project GetInfo(int ProjectId, string strConnect)
        {
            Cvk_Project info = new Cvk_Project();
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    info = db.Cvk_Projects.Single(p => p.ProjectId == ProjectId);
                }
                catch { info = null; }
                finally { db.Dispose(); }
            }
            return info;
        }
        public static List<Cvk_Project> GetList(string strConnect)
        {
            List<Cvk_Project> list = new List<Cvk_Project>();
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    var varList = from p in db.Cvk_Projects
                                  where p.IsDeleted == false
                                  select p;
                    list = varList.ToList();
                }
                catch { list = null; }
                finally { db.Dispose(); }
            }
            return list;
        }
        public static List<VG_Project> GetList(int FieldId, string strConnect)
        {
            List<VG_Project> list = new List<VG_Project>();
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    var varList = from p in db.VG_Projects
                                  where p.IsDeleted == false && p.IsActive == true && p.FieldId == FieldId
                                  select p;
                    list = varList.ToList();
                }
                catch { list = null; }
                finally { db.Dispose(); }
            }
            return list;
        }

        public static List<VG_Project> GetList(int fieldId, int provinceId, int wardId, string keyword, string strConnect)
        {
            List<VG_Project> list = new List<VG_Project>();
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    //var varList = from p in db.VG_Projects
                    //              where p.IsDeleted == false && p.IsActive == true && p.FieldId == FieldId
                    //              select p;
                    //list = varList.ToList();

                    string sql = "select * from VG_Projects where 1=1";
                    if (fieldId > 0)
                        sql += " and FieldId = " + fieldId.ToString();
                    if (provinceId > 0)
                        sql += " and CityId = " + provinceId.ToString();
                    if (wardId > 0)
                        sql += " and DistrictId = " + wardId.ToString();
                    if (!string.IsNullOrEmpty(keyword))
                        sql += " and ( Name like N'%" + keyword.Replace("'", "") + "%' or Info like N'%" + keyword.Replace("'", "") + "%')";
                    var varlist = db.ExecuteQuery<VG_Project>(sql);
                    list = varlist.ToList();
                }
                catch { list = null; }
                finally { db.Dispose(); }
            }
            return list;
        }

        public class claExecute
        {
            public static Cvk_Project_Execute GetInfo(int ProjectId, string strConnect)
            {
                Cvk_Project_Execute info = new Cvk_Project_Execute();
                using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
                {
                    try
                    {
                        info = db.Cvk_Project_Executes.Single(p => p.ProjectId == ProjectId);
                    }
                    catch { info = null; }
                    finally { db.Dispose(); }
                }
                return info;
            }
            public static int Insert(Cvk_Project_Execute info, string strConnect)
            {
                int result = 0;
                using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
                {
                    try
                    {
                        db.Cvk_Project_Executes.InsertOnSubmit(info);
                        db.SubmitChanges();
                        result = info.ProjectId;
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return result;
            }
            public static void Update(Cvk_Project_Execute info, string strConnect)
            {
                using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
                {
                    try
                    {
                        Cvk_Project_Execute infoOld = db.Cvk_Project_Executes.Single(p => p.ProjectId == info.ProjectId);
                        if (infoOld != null)
                        {
                            infoOld.BusinessId = info.BusinessId;
                            infoOld.FieldId = info.FieldId;
                            infoOld.CityId = info.CityId;
                            infoOld.DistrictId = info.DistrictId;
                            db.SubmitChanges();
                        }
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
            }
        }
    }
}
